package com.ysy.logistics.service.stock.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysy.logistics.entities.stock.GoodsCapacity;
import com.ysy.logistics.mapper.stock.GoodsCapacityMapper;
import com.ysy.logistics.service.stock.GoodsCapacityService;
import com.ysy.logistics.util.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/31 21:56
 **/
@Service
public class GoodsCapacityServiceImpl extends ServiceImpl<GoodsCapacityMapper, GoodsCapacity> implements GoodsCapacityService {

    @Resource
    private GoodsCapacityMapper goodsCapacityMapper;


    @Override
    public Page<GoodsCapacity> listByPage(PageInfo<GoodsCapacity> pageInfo) {
        return goodsCapacityMapper.selectByPage(pageInfo.getPage(), pageInfo.getCriteria());
    }

    @Override
    public boolean existsEntity(GoodsCapacity goodsCapacity) {
        LambdaQueryWrapper<GoodsCapacity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsCapacity::getGoodsName,goodsCapacity.getGoodsName())
                .eq(GoodsCapacity::getGoodsType,goodsCapacity.getGoodsType())
                .eq(GoodsCapacity::getPrice,goodsCapacity.getPrice())
                .eq(GoodsCapacity::getPriceUnit,goodsCapacity.getPriceUnit());
        if (CollectionUtils.isNotEmpty(goodsCapacity.getSpecification())) {
            //List转Json
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String json = objectMapper.writeValueAsString(goodsCapacity.getSpecification());
                queryWrapper.apply(" JSON_CONTAINS(specification,{0})",json);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

      return this.exists(queryWrapper);
    }

    @Override
    public GoodsCapacity getByEntity(GoodsCapacity goodsCapacity) {
        LambdaQueryWrapper<GoodsCapacity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsCapacity::getGoodsName,goodsCapacity.getGoodsName())
                .eq(GoodsCapacity::getGoodsType,goodsCapacity.getGoodsType())
                .eq(GoodsCapacity::getPrice,goodsCapacity.getPrice())
                .eq(GoodsCapacity::getPriceUnit,goodsCapacity.getPriceUnit());
        if (CollectionUtils.isNotEmpty(goodsCapacity.getSpecification())) {
            //List转Json
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String json = objectMapper.writeValueAsString(goodsCapacity.getSpecification());
                queryWrapper.apply(" JSON_CONTAINS(specification,{0})",json);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
        return this.getOne(queryWrapper);
    }
}
